package org.newbee;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectReader;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.newbee.entity.Users;

import java.io.IOException;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created by yujie on 2017/6/24.
 */
public class Test {
    public static void main(String[] args) {
        final SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();

        ExecutorService executorService = Executors.newFixedThreadPool(2);

        List<Users> list = new ArrayList<Users>();

        Future<?> future1 = executorService.submit(() -> {
            TenantIdResolver.tenantId.set("test");
            Session session = sessionFactory.openSession();
            Users users = session.find(Users.class, 1);
            if (null != users) {
                list.add(users);
            }
            session.close();
        });

        Future<?> future2 = executorService.submit(() -> {
            TenantIdResolver.tenantId.set("test1");
            Session session = sessionFactory.openSession();
            Users users = session.find(Users.class, 1);
            if (null != users) {
                list.add(users);
            }
            session.close();
        });

        //sessionFactory.close();
        while (!future1.isDone() && !future2.isDone()) {

        }
        if (null != list && list.size() > 0) {
            for (Users users : list) {
                System.out.println(users);
            }
        }
        executorService.shutdown();
    }
}
